<?php
	class dataservice
	{
		private $server	  = "127.0.0.1";
		private $username = "root";
		private $password = "vertrigo";
		private $database = "tintucbongda";
		private $m_link	  = false;
		
		public function __construct()
		{
			@$this->Connect($this->server, $this->username, $this->password, $this->database);
		}
		
		public function __destruct()
		{
			if($this->m_link)
				@mysql_close();
		}
		
		function Connect($i_server, $i_username, $i_password, $i_dbname)
		{
			@$this->m_link = mysql_connect($i_server, $i_username, $i_password);
			
			if($this->m_link)
				return mysql_select_db($i_dbname, $this->m_link);
			
			return false;
		}
		
		public static function QuoteSmart($i_value)
		{
			// Stripslashes
			if(get_magic_quotes_gpc())
				$i_value = stripslashes($i_value);
			
			// Quote if not integer
			if(!is_numeric($i_value))
				$i_value =  "'" . htmlspecialchars($i_value) . "'" ;
			
			return $i_value;
		}
		
		private function Execute($i_sql)
		{
			mysql_query("SET NAMES 'utf8'");
			
			$resource = mysql_query($i_sql);
			
			return $resource;
		}
		
		public function ExecuteNonQuery($i_sql)
		{
			$resource = $this->Execute($i_sql);
			
			if($resource)
				return true;
			else
				return false;
		}
		
		public function ExecuteQuery($i_sql)
		{
			$resource = $this->Execute($i_sql);
			
			$result_array = array();
			
			if($resource)
			{
				$num_rec = 0;
				
				while($row = mysql_fetch_array($resource))
				{
					$result_array[$num_rec] = $row;
					$num_rec++;
				}
				
				mysql_free_result($resource);
				
				if($num_rec > 0)
					return $result_array;
			}
			return false;
		}
		
		public function ExecuteScalar($i_sql)
		{
			$resource = $this->Execute($i_sql);
			
			if($resource)
			{
				$row = mysql_fetch_row($resource);
				
				mysql_free_result($resource);
				
				return $row[0];
			}
			return false;
		}
	}
?>